import { defineConfig, loadEnv } from 'vite'
import vue from '@vitejs/plugin-vue'
import AutoImport from 'unplugin-auto-import/vite'
import Components from 'unplugin-vue-components/vite'
import svgLoader from 'vite-svg-loader'
import {
  ElementPlusResolver
} from 'unplugin-vue-components/resolvers'
// 引入viteMockServe
import { viteMockServe } from 'vite-plugin-mock'

export default ({ mode }) => {
  console.log('=======配置文件 start==========')
  console.log(loadEnv(mode, process.cwd()))
  console.log('=======配置文件 end==========')
  return defineConfig({
    plugins: [
      vue(),
      // vite mock server
      // viteMockServe({
      //   // default
      //   supportTs: true,
      //   logger: false,
      //   mockPath: './mock/'
      // }),
      AutoImport({
        resolvers: [ElementPlusResolver()]
      }),
      Components({
        resolvers: [ElementPlusResolver()]
      }),
      svgLoader()
    ],
    server: {
      port: Number.parseInt(loadEnv(mode, process.cwd()).VITE_PORT),
      proxy: {
        [`${loadEnv(mode, process.cwd()).VITE_PROXY_DOMAIN}`]: {
          target: loadEnv(mode, process.cwd()).VITE_PROXY_DOMAIN_REAL, // 线上
          rewrite: (path:string) => path.replace(loadEnv(mode, process.cwd()).VITE_PROXY_DOMAIN, ''),
          changeOrigin: true,
          ws: true
        }
      }
    }
  })
}

// https://vitejs.dev/config/
// export default defineConfig({
//   plugins: [
//     vue(),
//     AutoImport({
//       resolvers: [ElementPlusResolver()]
//     }),
//     Components({
//       resolvers: [ElementPlusResolver()]
//     }),
//     svgLoader()
//   ],
//   // 服务端渲染
//   server: {
//     // 是否开启 https
//     https: false,
//     // 端口号
//     port: import.meta.env.VITE_PORT,
//     host: '0.0.0.0',
//     // 本地跨域代理
//     proxy:
//     import.meta.env.VITE_PROXY_DOMAIN_REAL.length > 0
//       ? {
//           target: import.meta.env.VITE_PROXY_DOMAIN_REAL,
//           // ws: true,
//           changeOrigin: true,
//           rewrite: (path: string) => regExps(path, import.meta.env.VITE_PROXY_DOMAIN)
//         }
//       : null
//   }
// })
